//
//  MainViewController.swift
//  Swift微博
//
//  Created by 陈竹 on 16/2/21.
//  Copyright © 2016年 陈竹. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        addChildViewControllers()
        setupTabbar()
        showNewFeature()
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        //把composeButton放到视图最上面
        tabBar.bringSubviewToFront(composeButton)
        
        //-1是为了防止边缘触摸到
        let width=tabBar.bounds.width/CGFloat(childViewControllers.count)-1
        
        //dx／dy  >0向内缩小
        //dx／dy  <0向外放大
        composeButton.frame=CGRectInset(tabBar.bounds, 2*width, 0)
        
        
    }
    
    @objc private func didClickComposeButton(){
        print("didClickComposeButton")
    }
    
    private lazy var composeButton=UIButton(title: nil,
        imageName: "tabbar_compose_icon_add",
        backImageName: "tabbar_compose_button")
    
    
}

extension MainViewController{
    
    private func showNewFeature(){
        //如果没登陆直接跳出
        if !UserAccount.sharedUserAccount.isLogin{
            return
        }
        let v=isNewVersion ? WelcomeView():NewFeatureView()
        view.addSubview(v)
        
    }
    private var isNewVersion:Bool{
     
        //获取当前版本号
        let currenVersion=NSBundle.mainBundle().infoDictionary?["CFBundleShortVersionString"] as?String
        let versionKey="cn.chenzhu.VersionKey"
        //取出已保存的版本号
        let sandboxVersion=NSUserDefaults.standardUserDefaults().stringForKey(versionKey)
        //保存当前版本号
        NSUserDefaults.standardUserDefaults().setObject(currenVersion, forKey: versionKey)

        print("\(sandboxVersion),\(currenVersion)")
        return sandboxVersion == currenVersion
    }
    
}

//MARK:- 设置tabbar
extension MainViewController{
    
    private func setupTabbar(){
        tabBar.shadowImage=UIImage.cz_singleDotImage(UIColor(white: 0.9, alpha: 1))
        tabBar.backgroundImage=UIImage(named: "tabbar_background")
        
        tabBar.addSubview(composeButton)
        //设置➕按钮的点击事件
        composeButton.addTarget(self, action:"didClickComposeButton", forControlEvents: .TouchUpInside)
        
    }
    
}


//MARK: - 添加子控件
extension MainViewController{
    
    
    private func addChildViewControllers() {
        addChildViewController(HomeViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageViewController(), title: "消息", imageName: "tabbar_message_center")
        
        addChildViewController(UIViewController())
        
        addChildViewController(DiscoverViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileViewController(), title: "我", imageName: "tabbar_profile")
        
    }
    
    private func addChildViewController(vc:UIViewController,title:String,imageName:String) {
        
        vc.title=title
        vc.tabBarItem.image=UIImage(named: imageName)
        //设置图片使用原图渲染
        vc.tabBarItem.selectedImage=UIImage(named:imageName+"_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        //设置字体选中颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: .Selected)
        
        let nav=UINavigationController(rootViewController: vc)
        addChildViewController(nav)
        
    }
    
    
}
